//
//  ST_TrophyViewController.swift
//  btvideo
//
//  Created by mac on 2021/7/7.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import JXSegmentedView

class ST_TrophyViewController: UIViewController {
    
    let segmented_data_source = JXSegmentedTitleDataSource()
    
    fileprivate lazy var segmentedView : JXSegmentedView = {
        let control = JXSegmentedView()
        control.delegate = self
        return control
    }()
    
    lazy var listContainerView: JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    fileprivate lazy var gradient_models = [ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.5882352941, green: 0.4980392157, blue: 1, alpha: 1),#colorLiteral(red: 0.7882352941, green: 0.6470588235, blue: 1, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1)),ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.8438429236, green: 0.6900256872, blue: 0.4820910692, alpha: 1),#colorLiteral(red: 0.9529411765, green: 0.8078431373, blue: 0.5882352941, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1)),ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.9960784314, green: 0.4235294118, blue: 0.4235294118, alpha: 1),#colorLiteral(red: 0.9960784314, green: 0.6862745098, blue: 0.6, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))]

    fileprivate lazy var gradient_view:ST_FWGradientView = {
       let control = ST_FWGradientView()
        if let item = gradient_models.first{
            control.model = item
        }
        return control
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        
        view.addSubview(gradient_view)
        gradient_view.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(300)
        }
        
        view.addSubview(own_navigation_bar)
        own_navigation_bar.snp.makeConstraints { (make) in
            make.right.left.top.equalTo(view)
            make.height.equalTo(statusBarHeight+44)
        }
        
        own_navigation_bar.addSubview(left_nav_btn)
        own_navigation_bar.addSubview(right_nav_btn)
        
        left_nav_btn.snp.makeConstraints { (make) in
            make.left.equalTo(own_navigation_bar).offset(8)
            make.bottom.equalTo(own_navigation_bar)
            make.width.height.equalTo(44)
        }
        
        right_nav_btn.snp.makeConstraints { (make) in
            make.right.equalTo(own_navigation_bar).offset(-14)
            make.bottom.equalTo(own_navigation_bar)
            make.width.height.equalTo(44)
        }
        

        
        let titles = ["女神榜", "富豪榜" ,"亲密榜"]
        
        segmented_data_source.titles = titles
        segmented_data_source.itemSpacing = TIANCompatibleValue(value: 45)
        segmented_data_source.itemWidth = 51
//        segmented_data_source.itemSpacing = 0
//        segmented_data_source.isTitleColorGradientEnabled = true
        segmented_data_source.titleNormalColor = UIColor.white
        segmented_data_source.titleSelectedColor = UIColor.white
        segmented_data_source.isTitleZoomEnabled = true
        segmented_data_source.titleSelectedZoomScale = 1.2
        
        segmentedView.dataSource = segmented_data_source
        segmentedView.contentEdgeInsetLeft = 0
        segmentedView.contentEdgeInsetRight = 0
        
        
        
        own_navigation_bar.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(65)
            make.right.equalToSuperview().offset(-65)
            make.centerY.equalTo(left_nav_btn)
        }
        
        segmentedView.backgroundColor = .clear
        segmentedView.listContainer = listContainerView
        view.addSubview(listContainerView)
        
        listContainerView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(own_navigation_bar.snp.bottom)
        }
        
    }

    lazy var own_navigation_bar:UIView = {
        let control = UIView()
        return control
    }()
    
    lazy var left_nav_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImage(ST_ImageUtils.imageName("返回_white"), for: UIControl.State.normal)
        
        control.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
        
//        control.backgroundColor = Color.random
        
        control.addTarget(self, action: #selector(left_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    lazy var right_nav_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImage(ST_ImageUtils.imageName("rank_rules"), for: .normal)
        control.addTarget(self, action: #selector(right_click), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    //    MARK: - Event
    @objc func right_click(){
        
        let vc = ST_TrophyRulesViewController()
       
       vc.overCurrentContext = true
       
       GlobalPresent(toController: vc)
        
    }
    @objc func left_click(){
        
        self.goback_previous_controller()
        
    }
}

extension ST_TrophyViewController: JXSegmentedListContainerViewDataSource {
    
    
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        let vc = ST_TrophyChildViewController()
        
        vc.titles = ["日榜", "周榜", "月榜"]
       
        vc.parentType = ST_TrophyRankType.allCases[safe:index]
        
        return vc
    }
}


extension ST_TrophyViewController:JXSegmentedViewDelegate{
    
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
        if let item = gradient_models[safe: index]{
            gradient_view.model = item
        }
        
    }
    
}
